package com.qfedu.entity.course12;

/**
 * 计数排序
 */
public class CountSort {
    public static int[] countSort(int[] array, int offset) {
        int[] count = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            count[array[i] - offset]++;
        }
        int[] result = new int[array.length];
        // i:计量数组下标 k:结果集下标
        for (int i = 0, k = 0; i < count.length; i++) {
            for (int j = 0; j < count[i]; j++) {
                result[k++] = i + offset;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        int[] scores = {95, 94, 91, 98, 99, 90, 99, 93, 91, 92};
        for (int n : countSort(scores, 90)) {
            System.out.println(n);
        }
    }
}

